package sunlight.util.config;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class RemoteConfigurationFile implements ConfigurationFile{
	
	private int Type;
	
	protected Properties Handler;
	protected URL 	     URI;

	public RemoteConfigurationFile(){
		this.Handler = new Properties();
	}
	
	public RemoteConfigurationFile(URL path){
		this.Handler = new Properties(); 
	}
	
	public RemoteConfigurationFile(URL path, int type) throws InvalidPropertiesFormatException, IOException{
		this.Handler = new Properties(); 
		this.Type 	 = type;
		this.URI  	 = path;
		
		if(this.Type  == RemoteConfigurationFile.CONFIG_FILE_XML){
			this.loadFromXML(this.URI.openConnection().getInputStream());
		}
		
		if(this.Type == RemoteConfigurationFile.CONFIG_FILE_PLAIN){
			this.loadFromPlainText(this.URI.openConnection().getInputStream());
		}
	}
	
	public void setTypeXML(){
		this.Type = RemoteConfigurationFile.CONFIG_FILE_XML;
	}
	
	public void setTypePlain(){
		this.Type = RemoteConfigurationFile.CONFIG_FILE_PLAIN;
	}
	
	public void loadFromXML(InputStream in) throws InvalidPropertiesFormatException, IOException{
		this.Handler.loadFromXML(in);
	}
	
	public void loadFromPlainText(InputStream in) throws IOException{
		this.Handler.load(in);
	}
	
	public Object get(String key){
		return this.Handler.get(key);
	}
	
	public Object get(String key, String defaultvalue){
		if(this.Handler.get(key) == null){
			return defaultvalue;
		}
		
		return this.Handler.get(key);
	}
}
